package com.poetry.controller.admin;

import com.poetry.domain.dto.CategoryDTO;
import com.poetry.domain.po.Category;
import com.poetry.exception.R;
import com.poetry.service.ICategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("/category")
@Tag(name = "分类管理模块")
public class CategoryController {
    @Resource
    private ICategoryService categoryService;

    @PostMapping("/create_category")
    @Operation(summary = "添加分类")
    public R<Integer> createCategory(@RequestParam String name) {
        Category category = new Category();
        category.setName(name);
        categoryService.save(category);
        return R.success("创建成功", category.getId());
    }

    @Operation(summary = "修改分类信息")
    @PostMapping("/update_category")
    public R<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        return categoryService.updateProperties(categoryDTO);
    }

    @PostMapping("/parent/{parentId},{childId}")
    @Operation(summary = "添加子类别")
    public R<Category> addChildCategory(@PathVariable Integer parentId,
                                     @PathVariable Integer childId) {
        return categoryService.addChildCategory(parentId, childId);
    }

    @DeleteMapping("/delete_category/{id}")
    @Operation(summary = "删除类别")
    public R<String> deleteCategory(@PathVariable("id") Integer id) {
        return categoryService.deleteCategory(id);
    }

    @GetMapping("/tree")
    @Operation(summary = "获取分类树")
    public R<List<Category>> getCategoryTree() {
        return categoryService.getCategoryTree();
    }

    @GetMapping("/get_category/{id}")
    @Operation(summary = "根据id获取分类信息")
    public R<Category> getCategory(@PathVariable Integer id) {
        return R.success("获取分类成功", categoryService.getById(id));
    }

}
